import { MigrationInterface, QueryRunner, Table } from 'typeorm';

export class AddCreditPackageTable1703000000005 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建信用包表
    await queryRunner.createTable(
      new Table({
        name: 'credit_packages',
        columns: [
          {
            name: 'id',
            type: 'uuid',
            isPrimary: true,
            generationStrategy: 'uuid',
            default: 'uuid_generate_v4()'
          },
          {
            name: 'name',
            type: 'varchar',
            length: '255'
          },
          {
            name: 'price',
            type: 'decimal',
            precision: 10,
            scale: 2
          },
          {
            name: 'credits',
            type: 'int'
          },
          {
            name: 'valid_days',
            type: 'int'
          },
          {
            name: 'popular',
            type: 'boolean',
            default: false
          },
          {
            name: 'features',
            type: 'jsonb',
            isNullable: true
          },
          {
            name: 'created_at',
            type: 'timestamp',
            default: 'now()'
          },
          {
            name: 'updated_at',
            type: 'timestamp',
            default: 'now()'
          }
        ]
      }),
      true
    );

    // 插入预定义的信用包数据
    await queryRunner.query(`
      INSERT INTO credit_packages (id, name, price, credits, valid_days, popular, features)
      VALUES 
        ('basic', 'Basic', 9.99, 10, 30, false, '["10 room redesigns", "Standard resolution images", "5 design styles", "Valid for 30 days"]'),
        ('pro', 'Pro', 19.99, 25, 60, true, '["25 room redesigns", "High resolution images", "All design styles", "Download options", "Valid for 60 days"]'),
        ('premium', 'Premium', 49.99, 75, 90, false, '["75 room redesigns", "Ultra-high resolution images", "All design styles", "Priority processing", "Download options", "Valid for 90 days"]')
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('credit_packages');
  }
}